package com.lizhivscaomei.wms.application.service.purchase.in;

import com.lizhivscaomei.wms.domain.entity.bills.inout.common.bill.StockBillPassedEvent;
import com.lizhivscaomei.wms.domain.entity.bills.inout.purchase.in.bill.PurchaseInBill;
import com.lizhivscaomei.wms.domain.entity.bills.inout.purchase.in.bill.PurchaseInBillRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

/**
 * 采购入库单
 */
@Service
public class PurchaseInBillManager {
    @Autowired
    private PurchaseInBillRepo purchaseInBillRepo;
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    public PurchaseInBill create() {
        return new PurchaseInBill();
    }

    public void save(PurchaseInBill bill) {
        this.purchaseInBillRepo.save(bill);
    }

    public void submit(PurchaseInBill bill) throws Exception {
        bill.submit();
        this.purchaseInBillRepo.save(bill);
    }

    public void with(PurchaseInBill bill) throws Exception {
        bill.withdraw();
        this.purchaseInBillRepo.save(bill);
    }

    public void reject(PurchaseInBill bill) throws Exception {
        bill.reject();
        this.purchaseInBillRepo.save(bill);
    }

    public void pass(PurchaseInBill bill) throws Exception {
        bill.pass();
        this.purchaseInBillRepo.save(bill);
        //发布审核事件
        this.applicationEventPublisher.publishEvent(new StockBillPassedEvent(this, bill));
    }
}
